import { TUIChatKit, genTestUserSig } from "../../index.ts";
import { vueVersion } from "../../adapter-vue";
import { TUILogin } from "@tencentcloud/tui-core";
import TencentCloudChat from '@tencentcloud/chat';

import {
	PERFIX,
	IM_SDKAppID,
	IM_secretKey,
} from '@/config/app';
import store from '@/store/index.js';
let chat = TencentCloudChat.create({
	SDKAppID: IM_SDKAppID // Replace `0` with the `SDKAppID` of your Chat app during access.
});
chat.setLogLevel(4);
store.commit('setImChat', chat) 
// TUIChatKit 初始化
TUIChatKit.init();
imEventListen();


function imEventListen() {
	chat.on(TencentCloudChat.EVENT.MESSAGE_RECEIVED, function (event) {
		console.log("MESSAGE_RECEIVED", event);
		getMsgCount()
		// 收到推送的单聊、群聊、群提示、群系统通知的新消息，可通过遍历 event.data 获取消息列表数据并渲染到页面
		// event.name - TencentCloudChat.EVENT.MESSAGE_RECEIVED
		// event.data - 存储 Message 对象的数组 - [Message]
	});
	chat.on(TencentCloudChat.EVENT.TOTAL_UNREAD_MESSAGE_COUNT_UPDATED, function (event) {
		console.log("onTotalUnreadMessageCountUpdated", event);
		getMsgCount()
	});
	// chat.on(TencentCloudChat.EVENT.SDK_READY, function() {
	// 	console.log("SDK_READY");
	// 	getMsgCount()
	// });
}
// 获取单聊和群聊会话的未读总数
function getMsgCount() {
	let totalUnreadCount = chat.getTotalUnreadMessageCount();
	store.commit('setImMsgCount', totalUnreadCount) //每次累加 5
}
const config = {
	userID: PERFIX + uni.getStorageSync('mobile'), // User ID
	SDKAppID: IM_SDKAppID, // Your SDKAppID
	secretKey: IM_secretKey, // Your secretKey
};
uni.$chat_userID = config.userID;
uni.$chat_SDKAppID = config.SDKAppID;
uni.$chat_secretKey = config.secretKey;
uni.$chat_userSig = genTestUserSig(config).userSig;

// login 
TUILogin.login({
	SDKAppID: uni.$chat_SDKAppID,
	userID: uni.$chat_userID,
	// UserSig 是用户登录即时通信 IM 的密码，其本质是对 UserID 等信息加密后得到的密文。
	// 该方法仅适合本地跑通 Demo 和功能调试，详情请参见 https://cloud.tencent.com/document/product/269/32688     
	userSig: uni.$chat_userSig,
	// 如果您需要发送图片、语音、视频、文件等富媒体消息，请设置为 true
	useUploadPlugin: true,
	// 本地审核可识别、处理不安全、不适宜的内容，为您的产品体验和业务安全保驾护航
	// 此功能为增值服务，请参考：https://cloud.tencent.com/document/product/269/79139
	// 如果您已购买内容审核服务，开启此功能请设置为 true
	useProfanityFilterPlugin: false,
	framework: `vue${vueVersion}` // 当前开发使用框架 vue2 / vue3
}).then(() => {
});